package camid.util.monitoring;

import java.util.concurrent.atomic.AtomicInteger;

import camid.distribution.events.FutureNotificator;
import camid.distribution.events.TimerEvent;

public class TimerAgent implements Runnable{

	private Thread thread;
	private AtomicInteger buffer;
	private FutureNotificator notificator;
	
	public TimerAgent(FutureNotificator notificator)
	{
		this.notificator = notificator;
		this.buffer = new AtomicInteger(1);
		this.thread = new Thread(this);
	}
	
	public void start()
	{
		this.thread.start();
	}
	
	
	public void shutdown()
	{
		buffer.set(0);
	}


	@Override
	public void run() 
	{
		while(buffer.get() != 0)
		{
			try
			{
				this.notificator.notifyEvent(new TimerEvent());
				Thread.sleep(1000);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}	
	}

	public Thread getThread() 
	{
		return thread;
	}
	
	
}
